package com.mjh.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import com.mjh.bean.Picture;
import com.mjh.comm.DBInstance;
import com.mjh.comm.page.Pager;

public class PictureDAO {

	public static Logger log = Logger.getLogger(PictureDAO.class);
	
	public static final String NAME_SPACE = "com.mjh.dao.mapper.picture";
	
	public int insert(Picture picture){
		SqlSession session = DBInstance.getInstance().getSession();
		try {
			return session.insert(NAME_SPACE + ".insert", picture);
		} catch (Exception e) {
			log.error(e.getMessage(), e.fillInStackTrace());
		} finally {
			session.commit();
			session.close();
		}
		return 0;
	}
	
	public int delete(int id){
		SqlSession session = DBInstance.getInstance().getSession();
		try {
			return session.update(NAME_SPACE + ".del", id);
		} catch (Exception e) {
			log.error(e.getMessage(), e.fillInStackTrace());
		} finally {
			session.close();
		}
		return 0;		
	}

	public int deleteForTrue(int id){
		SqlSession session = DBInstance.getInstance().getSession();
		try {
			return session.delete(NAME_SPACE + ".trueDel", id);
		} catch (Exception e) {
			log.error(e.getMessage(), e.fillInStackTrace());
		} finally {
			session.close();
		}
		return 0;		
	}
	
	public Picture select(int id) {
		SqlSession session = DBInstance.getInstance().getSession();
		Picture picture = null;
		try {
			picture = (Picture) session.selectOne(NAME_SPACE + ".selectOne", id);
		} catch (Exception e) {
			log.error(e.getMessage(), e.fillInStackTrace());
		} finally {
			session.close();
		}
		return picture;
	}
	
	@SuppressWarnings("unchecked")    
	public List<Picture> selectByArticle(int articleId, Pager pager){
		SqlSession session = DBInstance.getInstance().getSession();
		List<Picture> picture = new ArrayList<Picture>(1);
		HashMap<String,Integer> parameter = new HashMap<String,Integer>();
		parameter.put("articleId", new Integer(articleId));
		parameter.put("pageStart", pager.getStartNo());
		parameter.put("pageSize", pager.getPageSize());
		try {
			picture = session.selectList(NAME_SPACE + ".selectByArticle", parameter);
		} catch (Exception e) {
			log.error(e.getMessage(), e.fillInStackTrace());
		} finally {
			session.close();
		}
		return picture;
	}

	@SuppressWarnings("unchecked")    
	public List<Picture> selectAllByAdmin(Pager pager){
		SqlSession session = DBInstance.getInstance().getSession();
		List<Picture> picture = new ArrayList<Picture>(1);
		HashMap<String,Integer> parameter = new HashMap<String,Integer>();
		parameter.put("pageStart", pager.getStartNo());
		parameter.put("pageSize", pager.getPageSize());
		try {
			return  session.selectList(NAME_SPACE + ".selectAll", parameter);
		} catch (Exception e) {
			log.error(e.getMessage(), e.fillInStackTrace());
		} finally {
			session.close();
		}
		return picture;
	}
}
